<?php
echo "test";
require_once ROOT_PATH."/lib/models/leave/LeaveSummary.php";
$empInfo = null;
if (isset($records[count($records) - 1][0])) {
    $empInfo = $records[count($records) - 1][0];
}
array_pop($records);
$deletedLeaveTypesFound = false;
$auth = $modifier[1];
$dispYear = $modifier[2];
$copyQuota = $modifier[3];
$broughtForward = $modifier[4];
$modifier = $modifier[0];

if ($modifier === 'edit') {
    $btnImage = '../../themes/beyondT/pictures/btn_save.gif';
    $btnImageMO = '../../themes/beyondT/pictures/btn_save_02.gif';
    $frmAction = '?leavecode=Leave&action=Leave_Quota_Save';
} else {
    $btnImage = '../../themes/beyondT/pictures/btn_edit.gif';
    $btnImageMO = '../../themes/beyondT/pictures/btn_edit_02.gif';
    $frmAction = '?leavecode=Leave&action=Leave_Edit_Summary';
}

$backLink = "./CentralController.php?leavecode=Leave&action=Leave_Select_Employee_Leave_Summary";
$addExLeave = "./CentralController.php?leavecode=Leave&action=add_ex_leave";

/* Add sort parameters to form action url */
$sortBy = isset($_REQUEST['sortField']) ? $_REQUEST['sortField'] : null;
$sortOrder = null;
if ($sortBy != null) {
    $sortParam = "sortOrder" . $sortBy;
    if (isset($_REQUEST[$sortParam])) {
        $sortOrder = $_REQUEST[$sortParam];
    }
}
if ($sortBy != null && $sortOrder != null) {
    $frmAction .= "&sortField=${sortBy}&sortOrder${sortBy}=${sortOrder}";
}
require_once($lan->getLangPath("full.php"));
if ($empInfo[0] == $_SESSION['empID']) {
    $lang_Title = preg_replace(array('/#dispYear/'), array($dispYear), $lang_Leave_Leave_Summary_EMP_Title);
} else {
    if (isset($_REQUEST['id']) && ($_REQUEST['id'] != 0)) {
        $employeeName = $empInfo[2] . ' ' . $empInfo[1];
    } else {
        $employeeName = $lang_Leave_Common_AllEmployees;
    }
    $lang_Title = preg_replace(array('/#employeeName/', '/#dispYear/'), array($employeeName, $dispYear), $lang_Leave_Leave_Summary_SUP_Title);
}

function getCurSortOrder($colNum) {
    $curSortOrder = null;
    $varName = "sortOrder${colNum}";
    if (isset($_REQUEST[$varName])) {
        $curSortOrder = $_REQUEST[$varName];
    }
    return $curSortOrder;
}

function getNextSortOrder($colNum) {

    $curSortOrder = getCurSortOrder($colNum);

    if ($curSortOrder == 'ASC') {
        $nextSortOrder = "DESC";
    } else {
        $nextSortOrder = "ASC";
    }
    return $nextSortOrder;
}

function getNextSortOrderInWords($colNum) {
    $curSortOrder = getCurSortOrder($colNum);
    if ($curSortOrder == 'ASC') {
        return "lang_Common_Sort_DESC";
    } else {
        return "lang_Common_Sort_ASC";
    }
}

function getSortURL($colNum) {
    $sortOrder = getNextSortOrder($colNum);
    $url = "./CentralController.php?leavecode=Leave&action=Leave_Summary&sortField=${colNum}&sortOrder${colNum}=${sortOrder}";
    return $url;
}

function getSortIcon($colNum) {
    $imgName = getCurSortOrder($colNum);
    if ($imgName == null) {
        $imgName = 'null';
    }
    return "../../themes/beyondT/icons/" . $imgName . ".png";
}
?>
<?php include ROOT_PATH . "/lib/common/yui.php"; ?>

<style type="text/css">
    <!--
    .leaveQuotaLabel {
        padding-left: 2px;
        color: #FF0000;
        font-size: 11px;
    }

    .leaveQuotaBox {
        border: solid 1px #000000;
        padding: 2px;
        width: 40px;
    }
    -->
</style>

<script language="javascript">
    function init() {
        oLinkNewTimeEvent = new YAHOO.widget.Button("linkTakenLeave");
    }
    
    function validateLeaveQuotaAmount(strValue) {
        if (isNaN(strValue)) {
            return "<?php echo $lang_Leave_Summary_Error_NonNumericValue; ?>";
        } else {
            amount = new Number(strValue);
            if (amount > 365) {
                return "<?php echo $lang_Leave_Summary_Error_InvalidValue; ?>";
            }
        }
        return '';
    }

    function markFields(obj, msg) {
        if (msg != '') {
            obj.style.backgroundColor = '#FFCCCC';
        } else {
            obj.style.backgroundColor = '#FFFFFF';
        }

        labelIndex = obj.getAttribute('id');
        document.getElementById('leaveQuotaLabel_' + labelIndex).innerHTML = msg;
    }

    function validateLeaveSummary() {

        isValid = true;

        with (document.frmSummary) {
            for (i in elements) {
                if (elements[i] && elements[i].type == 'text') {
                    msg = validateLeaveQuotaAmount(elements[i].value);
                    markFields(elements[i], msg);

                    if (msg != '') {
                        isValid = false;
                    }
                }
            }
        }

        return isValid;
    }

    function validateIndividualLeaveQuota(obj) {
        msg = validateLeaveQuotaAmount(obj.value);
        markFields(obj, msg);
    }

    YAHOO.util.Event.addListener(window, "load", init);

    function actForm() {
        if (validateLeaveSummary()) {

            document.frmSummary.action = '<?php echo $frmAction; ?>';
            document.frmSummary.submit();
            return true;
        } else {
            //alert("<?php echo $lang_Leave_Summary_Error_CorrectLeaveSummary; ?>");
            return false;
        }
    }

    function goBack() {
    <?php if ($modifier === 'edit') { ?>
        document.frmSummary.reset();
        actForm();
    <?php } else { ?>
        location.href = '<?php echo $backLink; ?>';
    <?php } ?>
    }

    function sort(sortUrl) {
        document.frmSummary.action = sortUrl;
        document.frmSummary.submit();
    }
        
    function addExLeave(leaveTypeId,name){       
       var year = '<?php echo isset($_REQUEST['year']) ? $_REQUEST['year'] : date('Y'); ?>';
       var empId = '<?php echo $empInfo[0];?>';
       var url = '<?php echo $addExLeave ?>' + '&leaveTypeId='+leaveTypeId+ '&year='+year+ '&id='+empId+ '&name='+name ;       
       location.href = url;
    }

    <?php if ($auth === 'admin') { ?>
    function actTakenLeave() {
        document.frmSummary.action = '?leavecode=Leave&action=Leave_List_Taken';
        document.frmSummary.submit();
    }

    function actCopyLeaveQuota() {
        window.location = '?leavecode=Leave&action=Leave_Quota_Copy_Last_Year&currYear=<?php echo $dispYear; ?>';
    }

    function actCopyLeaveBroughtForward() {
        window.location = '?leavecode=Leave&action=Leave_Brought_Forward_Copy_Last_Year&currYear=<?php echo $dispYear; ?>';
    }
    <?php } ?>

    <?php if ($auth === 'supervisor') { ?>
    function actTakenLeave() {
        document.frmSummary.action = '?leavecode=Leave&action=Leave_List_Taken';
        document.frmSummary.submit();
    }    	
    <?php } ?>	
</script>

<h2><?php echo $lang_Title; ?><hr/></h2>
<?php
if (isset($_GET['message']) && $_GET['message'] != 'xx') {
    $expString = $_GET['message'];
    $expString = explode("_", $expString);
    $length = count($expString);
    $col_def = strtolower($expString[$length - 1]);
    $expString = 'lang_Leave_' . $_GET['message'];
    if (isset($$expString)) {
        ?>
        <font class="<?php echo $col_def ?>" size="-1" face="Verdana, Arial, Helvetica, sans-serif">
        <?php echo $$expString; ?>
        </font>
        <?php
    }
}
?>

<?php if (!is_array($records[0])) {?>
    <img title="Back" onMouseOut="this.src='../../themes/beyondT/pictures/btn_back.gif';" onMouseOver="this.src='../../themes/beyondT/pictures/btn_back_02.gif';"  src="../../themes/beyondT/pictures/btn_back.gif" onClick="goBack();">
    <h5><?php echo $lang_Error_NoRecordsFound; ?></h5>
<?php } else {?>
    <form method="post" onsubmit="return actForm(); return false;" name="frmSummary" id="frmSummary">
        <input type="hidden" name="id" value="<?php echo isset($_REQUEST['id']) ? $_REQUEST['id'] : LeaveQuota::LEAVEQUOTA_CRITERIA_ALL; ?>"/>        
        <input type="hidden" name="leaveTypeId" value="<?php echo isset($_REQUEST['leaveTypeId']) ? $_REQUEST['leaveTypeId'] : LeaveQuota::LEAVEQUOTA_CRITERIA_ALL; ?>" />
        <input type="hidden" name="year" value="<?php echo isset($_REQUEST['year']) ? $_REQUEST['year'] : date('Y'); ?>" />
        <input type="hidden" name="searchBy" value="<?php echo isset($_REQUEST['searchBy']) ? $_REQUEST['searchBy'] : "employee"; ?>"/>

        <?php if ($auth === 'admin') { ?>
            <p class="controls">
                <img style="float:left" title="Back" onMouseOut="this.src='../../themes/beyondT/pictures/btn_back.gif';" onMouseOver="this.src='../../themes/beyondT/pictures/btn_back_02.gif';"  src="../../themes/beyondT/pictures/btn_back.gif" onClick="goBack();">
                <input style="float:left" type="image" name="btnAct" src="<?php echo $btnImage; ?>" onMouseOut="this.src='<?php echo $btnImage; ?>';" onMouseOver="this.src='<?php echo $btnImageMO; ?>';" />
                <?php if (isset($_REQUEST['id']) && ($_REQUEST['id'] != LeaveQuota::LEAVEQUOTA_CRITERIA_ALL)) { ?>
                    <input type="button" onclick="javascript:actTakenLeave()" value="<?php echo $lang_Leave_Common_ListOfTakenLeave; ?>"/>
                    <input type="button" onclick="javascript:addExLeave('LTY007','Compensation')" value="Add Compensation Entitled"/>
                    <!--<input type="button" onclick="javascript:addExLeave('LTY009','Time Shift')" value="Add Time-shift Entitled"/>-->
                <?php } ?>	
            </p>
        <?php } else if ($auth === 'supervisor') {?>		
            <p class="controls">
                <img title="Back" onMouseOut="this.src='../../themes/beyondT/pictures/btn_back.gif';" onMouseOver="this.src='../../themes/beyondT/pictures/btn_back_02.gif';"  src="../../themes/beyondT/pictures/btn_back.gif" onClick="goBack();">
                <?php if (isset($_REQUEST['id']) && ($_REQUEST['id'] != LeaveQuota::LEAVEQUOTA_CRITERIA_ALL)) { ?>
                    <input type="button" onclick="javascript:actTakenLeave()" value="<?php echo $lang_Leave_Common_ListOfTakenLeave; ?>"/>                    
                <?php } ?>
            </p>
        <?php } ?>
            <br>    
        <table id="leave_quota" border="0" cellpadding="0" cellspacing="0" style="float:left">
            <thead>
                <tr>
                    <th class="tableTopLeft"></th>
                    <?php if ((isset($_REQUEST['id']) && empty($_REQUEST['id'])) && (!isset($_SESSION['empID']) || (isset($_SESSION['empID']) && ($empInfo[0] != $_SESSION['empID'])))) { ?>
                        <th class="tableTopMiddle"></th>
                    <?php } ?>
                    <th class="tableTopMiddle"></th>
                    <?php if ($auth === 'admin') { ?>
                        <th class="tableTopMiddle"></th>
                    <?php } ?>
                    <th class="tableTopMiddle"></th>
                    <th class="tableTopMiddle"></th>
                    <th class="tableTopMiddle"></th>
                    <th class="tableTopRight"></th>
                </tr>
                <tr>
                    <th class="tableMiddleLeft"></th>

                    <?php if ((isset($_REQUEST['id']) && empty($_REQUEST['id'])) 
                               && (!isset($_SESSION['empID']) || (isset($_SESSION['empID']) 
                               && ($empInfo[0] != $_SESSION['empID'])))) { ?>                        
                        <th width="180px" class="tableMiddleMiddle">
                            <?php echo $lang_Leave_Common_EmployeeName; ?>
                        </th>
                    <?php } ?>                                            
                    
                    <th width="100px" class="tableMiddleMiddle">
                        Leave Type 
                    </th>

                    <?php if ($auth === 'admin') { ?>                    
                    <th width="120px" class="tableMiddleMiddle">
                        Entitled (days)
                    </th>
                    <?php } ?>
                    
                   
                    <th width="100px" class="tableMiddleMiddle">                   
                        Taken (days)                        
                    </th>
                    <th width="180px" class="tableMiddleMiddle">                    
                        Scheduled (days)                     
                    </th>
                    <th width="180px" class="tableMiddleMiddle">                    
                       Remaining (days)                         
                    </th>
                    <th class="tableMiddleRight"></th>
                </tr>
            </thead>
            
            <tbody>
            <?php
            $leaveTypeObj = new LeaveType();
            $j = 0;
            if (is_array($records[0])) {
                $quota = LeaveSummary::parseCompensationLeave($records[0]);
                foreach ($records[0] as $record) {
                    $cssClass = (!($j % 2)) ? 'odd' : 'even';
                    $j++;
                    if ($record['available_flag'] == $leaveTypeObj->availableStatusFlag) {
                        $deletedLeaveType = false;
                    } else {
                        $deletedLeaveTypesFound = true;
                        $deletedLeaveType = true;
                    }
                    ?>
                    <tr>
                        <td class="tableMiddleLeft"></td>
                        <?php if ((isset($_REQUEST['id']) && empty($_REQUEST['id'])) && (!isset($_SESSION['empID']) || (isset($_SESSION['empID']) && ($empInfo[0] != $_SESSION['empID'])))) { ?>
                        <td class="<?php echo $cssClass; ?>">
                            <?php echo $record['employee_name'] ?>
                        </td>
                        <?php } ?>
                        
                        <td class="<?php echo $cssClass; ?>">
                            <?php echo $record['leave_type_name'];
                            if ($deletedLeaveType) { echo '<span class="error">*</span>';}?>
                        </td>
                        
                        <?php if (($auth === 'admin') && ($modifier === 'display')) { ?>
                        <td class="<?php echo $cssClass; ?>">
                            <?php echo number_format(round($record['no_of_days_allotted'], 2), 2); ?>
                        </td>
                        <?php } else if (($auth === 'admin') && ($modifier === 'edit')) {
                             $readOnly = ($deletedLeaveType) ? 'readonly="readonly"' : '';?>
                        <td class="<?php echo $cssClass; ?>">
                            <input type="hidden" name="txtLeaveTypeName[]" value="<?php echo $record['leave_type_name']; ?>"/>
                            <input type="hidden" name="txtLeaveTypeId[]" value="<?php echo $record['leave_type_id']; ?>"/>
                            <input type="hidden" name="txtEmployeeId[]" value="<?php echo $record['emp_number']; ?>"/>
                            <input type="hidden" name="txtquotaId[]" value="<?php echo $record['quota_id']; ?>"/>
                            <input type="text" name="txtLeaveEntitled[]" class="leaveQuotaBox"
                                   id="<?php echo $j; ?>" value="<?php echo number_format(round($record['no_of_days_allotted'], 2), 2); ?>"
                                   onblur="validateIndividualLeaveQuota(this)"
                                   <?php echo $readOnly; ?> />
                            <span id="leaveQuotaLabel_<?php echo $j; ?>" class="leaveQuotaLabel"></span>
                        </td>                        
                        <?php } ?>
                        
                        <td class="<?php echo $cssClass; ?>">
                            <?php if (!empty($record['leave_taken'])) {
                                echo number_format(round($record['leave_taken'], 2), 2);
                            } else {
                                echo "0.00";
                            }?>
                        </td>
                        
                        <td class="<?php echo $cssClass; ?>">
                            <?php if (!empty($record['leave_scheduled'])) {
                                if($record['leave_type_name']=='Compensation'){                                    
                                    echo isset($quota[$record['quota_id']]['scheduled'])?number_format(round($quota[$record['quota_id']]['scheduled'], 2), 2):"0.00";
                                }else{
                                    echo number_format(round($record['leave_scheduled'], 2), 2);
                                }
                            } else { echo "0.00"; }?>
                        </td>

                        <td class="<?php echo $cssClass; ?>">
                            <?php                           
                            if (!empty($record['leave_available'])) {
                                if($record['leave_type_name']=='Compensation'){                                                                        
                                    $leaveAvailable = isset($quota[$record['quota_id']]['remain'])?number_format(round($quota[$record['quota_id']]['remain'], 2), 2):"0.00";
                                }else{
                                    $leaveAvailable = number_format(round(($record['no_of_days_allotted'] - $record['leave_taken'] - $record['leave_scheduled']), 2), 2);
                                }
                                
                                echo $leaveAvailable;
                                //if ($leaveAvailable > 0) {
                                    $updated_at = new DateTime($record['updated_at']);
                                    if ($record['leave_type_name'] == 'Compensation') {
                                        $updated_at->modify('+2 month');
                                        if($record['no_of_days_allotted'] != 0){
                                            echo ' (expire:' . $updated_at->format('d-M-y') . ')';
                                        }    
                                    }
                                //}
                            }?>
                        </td>                        
                        <td class="tableMiddleRight"></td>
                    </tr>
                <?php } //End for?>
            <?php } //End if?>
            </tbody>
            <tfoot>
                <tr>
                    <td class="tableBottomLeft"></td>
                    <?php if ((isset($_REQUEST['id']) && empty($_REQUEST['id'])) && (!isset($_SESSION['empID']) || (isset($_SESSION['empID']) && ($empInfo[0] != $_SESSION['empID'])))) { ?>
                    <td class="tableBottomMiddle"></td>
                    <?php } ?>
                    <td class="tableBottomMiddle"></td>
                    <?php if ($auth === 'admin') { ?>
                    <td class="tableBottomMiddle"></td>
                    <?php } ?>
                    <td class="tableBottomMiddle"></td>
                    <td class="tableBottomMiddle"></td>
                    <td class="tableBottomMiddle"></td>
                    <td class="tableBottomRight"></td>
                </tr>
            </tfoot>
        </table>
    </form>
    <?php
    if ($deletedLeaveTypesFound) {
        include ROOT_PATH . "/templates/leave/deletedLeaveInfo.php";
    }
}
?>
